<?php
namespace App\Services\Area;
use App\Repositories\AddressRepository;

/**
 * Each engineer has a duty to keep the code elegant
 * Created by wolffy.
 * Author @wolffy<13750088090@sina.cn>
 * Date: 2018/11/27
 * Time: 14:07
 */
class AreaService
{
    protected $addressRepository;

    public function __construct(AddressRepository $addressRepository)
    {
        $this->addressRepository = $addressRepository;
    }
    public function getPCA()
    {
        return file_get_contents(__DIR__ . '/pca.json');
    }

    public function getPCACode()
    {
        return file_get_contents(__DIR__ . '/pca-code.json');
    }

    public function findCode($address)
    {
        $data = [];
        $province = $this->addressRepository->byNamePid($address['province']);
        if (!empty($province)) {
            $city = $this->addressRepository->byNamePid($address['city'], $province->id);
            if (!empty($city)) {
                $area = $this->addressRepository->byNamePid($address['area'], $city->id);
                if (!empty($area)) {
                    $data =  [
                        'province' => $province->name,
                        'province_code' => $province->code,
                        'city' => $city->name,
                        'city_code' => $city->code,
                        'area' => $area->name,
                        'area_code' => $area->code,
                    ];
                }
            }
        } else {
            $data =  [
                'province' => '广东省',
                'province_code' => 44,
                'city' => '广州市',
                'city_code' => 4401,
                'area' => '天河区',
                'area_code' => 440106,
            ];
        }
        return serialize($data);
    }

    public function create()
    {
        $json = file_get_contents(__DIR__ . '/pca-code.json');
        $data = json_decode($json, true);
        foreach ($data as $value) {
            $province = [
                'code' => $value['code'],
                'name' => $value['name'],
                'pid' => 0,
                'created_at' => date('Y-m-d H:i:s'),
                'updated_at' => date('Y-m-d H:i:s'),
            ];
            $provinceId = $this->addressRepository->insertGetId($province);
            foreach ($value['children'] as $item) {
                $city = [
                    'code' => $item['code'],
                    'name' => $item['name'],
                    'pid' => $provinceId,
                    'created_at' => date('Y-m-d H:i:s'),
                    'updated_at' => date('Y-m-d H:i:s'),
                ];
                $cityId = $this->addressRepository->insertGetId($city);
                foreach ($item['children'] as $val) {
                    $area = [
                        'code' => $val['code'],
                        'name' => $val['name'],
                        'pid' => $cityId,
                        'created_at' => date('Y-m-d H:i:s'),
                        'updated_at' => date('Y-m-d H:i:s'),
                    ];
                    $areaId = $this->addressRepository->insertGetId($area);
                }
            }
        }
    }





}